package com.hjt.datastructures.queue;

/**
 * @Author: zt49090
 * @CreateTime: 2021/8/13 10:13
 * @Description:数组模拟队列
 */
public class ArrayQueueDemo {
    public static void main(String[] args) {
        ArrayQueue queue = new ArrayQueue(3);
        queue.add(10);
        queue.add(20);
        queue.add(30);

//        int i = queue.get();
//        queue.show();
        int head = queue.head();
        System.out.println(head);
    }
}

class ArrayQueue {
    int maxSize;
    int front;//指向队列头
    int rear;//指向队列尾
    int[] arr;

    //构造方法
    public ArrayQueue(int arrMaxSize) {
        maxSize = arrMaxSize;
        front = -1;//指向队列头,队列头的前一个位置
        rear = -1;//指向队列尾,指向队列的最后一个数据
        arr = new int[maxSize];
    }

    //判断是否为空
    public boolean isEmpty() {
        return front == rear;
    }

    //判断队列是否满
    public boolean isFull() {
        return rear == maxSize - 1;
    }

    //添加数据
    public void add(int n) {
        if (isFull()) {
            System.out.println("队列已满无法添加数据");
            return;
        }
        rear++;//尾部指针后移
        arr[rear] = n;
    }

    //取出数据
    public int get() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法取出数据");
        }
        front++;
        return arr[front];
    }

    //显示队列
    public void show() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法取出数据");
        }
        for (int i : arr) {
            System.out.printf("%d\n", i);
        }
    }

    //显示队列头
    public int head() {
        if (isEmpty()) {
            throw new RuntimeException("队列为空,无法头部数据");
        }
        return arr[front + 1];
    }
}
